<?php
/**
 * @name AdminCommonController
 * @desc 控制器
 */
class AdminCommonController extends ApiBaseController {

    public $admin_info = [];

    public function init() {
        parent::init();
        $config = \Yaf\Application::app()->getConfig()->app;
        if($config['is_token_auth'] == 1){
            $r_token = $this->getRequest()->getServer('HTTP_X_AUTH_TOKEN');
//            $r_token = '566C7046556D39694D555A774D3035555A4570695656704F636C6C735A484E6B566D396E65574A4864457068545730354E465246546B74715957315353474A49556B4668565442724D6C525765464A575A56553552564E5056453550566B5A724D454655624764335A465531615441776156686B5233526F555652704D4442704D446B3D';
            if(!$r_token || $r_token == 'null'){
                $this->ajaxReturn(403,"请先登录");
            }
            $tokenData = \Vendor\Kstring::getTokenStr($r_token);
            $tokenData = json_decode($tokenData, true);
            if(empty($tokenData) || !isset($tokenData['admin_id']) || !isset($tokenData['ctime'])){
                $this->ajaxReturn(403, "登录失败");
            }
            if(time()-$tokenData['ctime']> 3600*24*30){
                $this->ajaxReturn(403, "登录已过期");
            }
            $adminModel = AdminModel::getSingleton();
            $info = $adminModel->db
                ->field('a.admin_id,a.admin_type,a.status,u.user_id,u.user_type,u.shop_id,u.shop_user_id')
                ->alias('a')
                ->join(['user', 'u'], ['a.user_id'=> 'u.user_id'], 'left join')
                ->where_c(['a.admin_id'=> $tokenData['admin_id']])
                ->find();
            if($info['status'] == 0){
                $this->ajaxReturn(400, '账号被禁用');
            }
            $this->admin_info = $info;
        }
        if(empty($this->admin_info)){
            $this->ajaxReturn(403,"请先登录");
        }
    }
}
